package com.foreveross.taskservice.application.impl.statistics;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.taskservice.application.channel.IGradChannelApplication;
import com.foreveross.taskservice.application.statistics.EngineStatisticsHistoryApplication;
import com.foreveross.taskservice.application.task.statsdetail.vo.CrawlStatsVO;
import com.foreveross.taskservice.core.domain.channel.GradChannelEntity;
import com.foreveross.taskservice.core.domain.statitics.entity.StaticHistoryTotalEntity;
import com.foreveross.taskservice.infra.util.DateUtil;
import com.google.common.collect.Lists;
@Transactional
@Scope("singleton")
@Service("engineStatisticsHistoryApplication")
public class EngineStatisticsHistoryApplicationImpl implements EngineStatisticsHistoryApplication{
	
	@Resource(name="gradChannelApplicationImpl")
	private IGradChannelApplication gradChannelApplication;

	@Override
	public void addDataByDate(List<CrawlStatsVO> listVo) {
		StaticHistoryTotalEntity tp=new StaticHistoryTotalEntity();
		List<StaticHistoryTotalEntity> listEntity =new ArrayList<StaticHistoryTotalEntity>();
		
		List<GradChannelEntity> channels=Lists.newArrayList();
		try {
			channels = gradChannelApplication.getEnableChannel();
		} catch (Exception e) {
			e.printStackTrace();
		}		
		for(CrawlStatsVO vo:listVo){
			StaticHistoryTotalEntity entity=new StaticHistoryTotalEntity();
			entity.setChannelId(vo.getChannelId());
			entity.setChannelName(vo.getChannelName());
			entity.setCreateTime(new Date());
			entity.setDoingNum(vo.getDoingStatusCount());
			entity.setGrabDate(vo.getCrawlDateStr());
			entity.setGrabFailureNum(vo.getFetchFailStatusCount());
			entity.setOtherFailureNum(vo.getOtherFailStatusCount());
			entity.setSuspendingNum(vo.getPauseStatusCount());
			entity.setTotalCountNum(vo.getCrawlCount());
			entity.setTotalNum(vo.getSum());
			entity.setChannelCode(getHbaseKey(vo.getChannelId(), channels));
			entity.setTotalSuccessNum(vo.getSuccessStatusCount());
			entity.setTotalSourceSize(vo.getBothByteLength());
			entity.setBusinessFailNum(vo.getBusinessFailStatusCount());
			entity.setBusinessSuccessNum(vo.getBusinessSuccessStatusCount());
			entity.setWaittingNum(vo.getWaitStatusCount());
			entity.setAnalysisFailureNum(vo.getParaseFailStatusCount());
			listEntity.add(entity);
		}
		tp.addEntity(listEntity);
	}

	@Override
	public List<CrawlStatsVO> findVoByDate(String startDateStr,String endDateStr){
		List<CrawlStatsVO> listvo= new ArrayList<CrawlStatsVO>();
		StaticHistoryTotalEntity tp=new StaticHistoryTotalEntity();
		List<StaticHistoryTotalEntity> listEntity =new ArrayList<StaticHistoryTotalEntity>();
		listEntity=tp.findVoByDate(startDateStr,endDateStr);
		if(listEntity.size()>0){
			for(StaticHistoryTotalEntity entity: listEntity){
				CrawlStatsVO vo=new CrawlStatsVO(DateUtil.StringToDate("yyyy-MM-dd", entity.getGrabDate()),
						entity.getChannelId(), entity.getChannelName(),entity.getChannelCode(), 
						 entity.getSuspendingNum(), 
						entity.getOtherFailureNum(), entity.getDoingNum(), 
						entity.getGrabFailureNum(), entity.getWaittingNum(), 
						entity.getDoingNum(), entity.getBusinessFailNum(),
						entity.getBusinessSuccessNum(),entity.getTotalSuccessNum(), entity.getTotalSourceSize(), 
						entity.getTotalCountNum());
				listvo.add(vo);
			}
		}
		
		return listvo;
	}

	
	@Override
	public List<CrawlStatsVO> findVoByDate(String data) {
		List<CrawlStatsVO> listvo= new ArrayList<CrawlStatsVO>();
		StaticHistoryTotalEntity tp=new StaticHistoryTotalEntity();
		List<StaticHistoryTotalEntity> listEntity =new ArrayList<StaticHistoryTotalEntity>();
		
		listEntity=tp.findVoByDate(data);
		if(listEntity.size()>0){
			for(StaticHistoryTotalEntity entity: listEntity){
				CrawlStatsVO vo=new CrawlStatsVO(DateUtil.StringToDate("yyyy-MM-dd", entity.getGrabDate()),
						entity.getChannelId(), entity.getChannelName(),entity.getChannelCode(), 
						 entity.getSuspendingNum(), 
						entity.getOtherFailureNum(), entity.getDoingNum(), 
						entity.getGrabFailureNum(), entity.getWaittingNum(), 
						entity.getDoingNum(), entity.getBusinessFailNum(),
						entity.getBusinessSuccessNum(),entity.getTotalSuccessNum(), entity.getTotalSourceSize(), 
						entity.getTotalCountNum());
				listvo.add(vo);
			}
		}
		
		return listvo;
	}
		

	//hbaseKey
	public static String getHbaseKey(Long id,List<GradChannelEntity> channels){
		String hbaseKey=null;
		if(channels.size()>0){
			for(GradChannelEntity e:channels){
				if(id==e.getId()){
					hbaseKey=e.getHbaseKey();
					break;
				}
			}
		}
		return hbaseKey;
	}

	
}
